package com.neusoft.restaurant.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neusoft.restaurant.config.utils.PageUtils;
import com.neusoft.restaurant.entity.PageResult;
import com.neusoft.restaurant.entity.ResAddress;
import com.neusoft.restaurant.mapper.ResAddressMapper;
import com.neusoft.restaurant.service.IResAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 地址管理Service业务层处理
 * 
 * @author zhongda
 * @date 2023-12-10
 */
@Service
public class ResAddressServiceImpl implements IResAddressService
{
    @Autowired
    HttpServletRequest request;
    @Autowired
    private ResAddressMapper resAddressMapper;

    /**
     * 查询地址管理
     * 
     * @param id 地址管理主键
     * @return 地址管理
     */
    @Override
    public ResAddress selectResAddressById(Long id)
    {
        return resAddressMapper.selectResAddressById(id);
    }

    /**
     * 查询地址管理列表
     * 
     * @param resAddress 地址管理
     * @return 地址管理
     */
    @Override
    public List<ResAddress> selectResAddressList(ResAddress resAddress)
    {
        return resAddressMapper.selectResAddressList(resAddress);
    }

    @Override
    public PageResult selectResAddressList2(ResAddress resAddress, Integer pageNum, Integer pageSize) {
        //1、PageHelper开始分页，传入第几页和每页多少条记录两个参数
        PageHelper.startPage(pageNum, pageSize);
        //2、数据库进行查询
        List<ResAddress> resAddresses = resAddressMapper.selectResAddressList(resAddress);
        //3、对返回值进行判断
        if (resAddresses.size()==0){
            return null;
        }
        //4、对查询的结果进行封装
        PageInfo<ResAddress> resAddressPageInfo = new PageInfo<>(resAddresses);
        PageResult pageResult = PageUtils.getPageResult(resAddressPageInfo);

        return pageResult;
    }

    /**
     * 新增地址管理
     * 
     * @param resAddress 地址管理
     * @return 结果
     */
    @Override
    public int insertResAddress(ResAddress resAddress)
    {
       Long userId= (Long) request.getAttribute("userId");
//        System.out.println("获取当前用户id--->"+userId);
        resAddress.setUserId(userId);
        resAddress.setCreateTime(new Date());
        return resAddressMapper.insertResAddress(resAddress);
    }

    /**
     * 修改地址管理
     * 
     * @param resAddress 地址管理
     * @return 结果
     */
    @Override
    public int updateResAddress(ResAddress resAddress)
    {
        resAddress.setUpdateTime(new Date());
        return resAddressMapper.updateResAddress(resAddress);
    }

    /**
     * 批量删除地址管理
     * 
     * @param ids 需要删除的地址管理主键
     * @return 结果
     */
    @Override
    public int deleteResAddressByIds(Long[] ids)
    {
        return resAddressMapper.deleteResAddressByIds(ids);
    }

    /**
     * 删除地址管理信息
     * 
     * @param id 地址管理主键
     * @return 结果
     */
    @Override
    public int deleteResAddressById(Long id)
    {
        return resAddressMapper.deleteResAddressById(id);
    }
}
